<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>首页</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" th:href="@{/api/oauth/static/layui/css/layui.css}" media="all">
</head>
<body>

<style>
    /** 滚动条样式 */
    ::-webkit-scrollbar {
        width: 10px;
        height: 10px;
        background: transparent;
    }

    ::-webkit-scrollbar-track {
        background: transparent;
    }

    ::-webkit-scrollbar-thumb {
        border-radius: 5px;
        background-color: #C1C1C1;
    }

    ::-webkit-scrollbar-thumb:hover {
        background-color: #A8A8A8;
    }

    .mini-bar::-webkit-scrollbar {
        width: 6px;
        height: 6px;
    }

    .mini-bar::-webkit-scrollbar-thumb {
        border-radius: 3px;
    }

    @media screen and (max-width: 768px) {
        ::-webkit-scrollbar {
            width: 4px;
            height: 4px;
        }

        ::-webkit-scrollbar-thumb {
            border-radius: 0;
        }
    }

    /* laypage */
    .layui-laypage .layui-laypage-curr .layui-laypage-em {
        background-color: #1E9FFF;
    }

    .layui-laypage input:focus,
    .layui-laypage select:focus {
        border-color: #1E9FFF !important;
    }

    .layui-laypage a:hover {
        color: #1E9FFF;
    }

    /* 为table的所有单元格添加换行样式 */
    .layui-table td, .layui-table th {
        white-space: normal;

        word-break: break-all;
    }
</style>

<ul class="layui-nav layui-bg-blue" lay-bar="disabled" style="display: flex;justify-content: space-between;align-items: center;">
    <li class="layui-nav-item"><a href="">统一开放互联平台</a></li>
<!--    <li class="layui-nav-item"><a href="#"></a></li>-->
    <li class="layui-nav-item"><a href="javascript:;">[[${username}]]/[[${name}]]</a>
        <dl class="layui-nav-child">
            <dd><a href="#" lay-on="logout" >注销登录</a></dd>
        </dl>
    </li>
</ul>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layui-form-pane" lay-filter="LAY-client-front-form">
            <div class="layui-form-item">
                <input type="hidden" name="userId" th:value="${userId}" />
            </div>
        </div>
        <div class="layui-card-body">
            <div class="layui-btn-group" style="padding-bottom: 10px;">
                <!--刷新table-->
                <button type="button" class="layui-btn layui-btn-normal layui-btn-sm" lay-submit lay-filter="LAY-client-refresh">
                    <i class="layui-icon">&#xe666;</i>刷新
                </button>
                <!-- 新增 -->
                <button type="button" class="layui-btn layui-btn-normal layui-btn-sm" lay-submit lay-filter="LAY-client-add">
                    <i class="layui-icon">&#xe654;</i>新增
                </button>
            </div>

            <!--table 定义-->
            <table id="LAY-client-table" lay-filter="LAY-client-table"></table>

            <!--列表操作项-->
            <script type="text/html" id="client-table">
                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
            </script>
        </div>
    </div>
</div>

<!-- HTML Content -->
<script th:inline="javascript">
    const userId = [[${userId}]];
</script>
<script th:src="@{/api/oauth/static/layui/layui.js}"></script>
<script th:src="@{/api/oauth/static/layui/jquery.js}"></script>
<script th:inline="javascript">
    layui.use(function () {
        let layer = layui.layer;
        let $ = layui.jquery;
        let form = layui.form;
        let util = layui.util;
        let table = layui.table;

        let protocol = window.location.protocol;
        let hostname = window.location.hostname;
        let port = window.location.port;
        let path = window.location.pathname;
        let search = window.location.search

        let url = `${protocol}//${hostname}:${port}${path}${search}`;
        let ctx;
        if (path.startsWith('/api')) {
            ctx = '/api';
        } else {
            ctx = '';
        }

        // 创建渲染实例
        table.render({
            elem: '#LAY-client-table',
            url: ctx + '/oauth/client/page',
            method: 'post',
            contentType: 'application/json',
            headers: {'Access_Token': sessionStorage.getItem('Access_Token')},
            where: {
                param: {
                    tenantId: userId
                }
            },
            page: true,
            limit: 10,
            limits: [5, 10, 15, 20, 25, 30, 50, 100],
            request: {
                pageName: 'pageNum', // 页码的参数名称，默认：page
                limitName: 'pageSize', //每页数据量的参数名，默认：limit
            },
            response: {
                statusCode: '00000' // 重新规定成功的状态码为 00000，table 组件默认为 0
            },
            // 将原始数据解析成 table 组件所规定的数据格式
            parseData: function (res) {
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": res.total, //解析数据长度
                    "data": res.data //解析数据列表
                }
            },
            // 注意这里的中括号要换行，不然会报错
            cols: [
                [
                    {type: 'numbers', fixed: 'left', title: '序号'},
                    {field: 'appName', title: '应用名称', minWidth: 100},
                    {field: 'clientId', title: '客户端ID', minWidth: 200},
                    {field: 'appSecret', title: '客户端密钥', minWidth: 200},
                    // {field: 'scope', title: '权限范围', minWidth: 100},
                    // {field: 'resourceIds', title: '资源ID集合', minWidth: 100},
                    // {field: 'authorizedGrantTypes', title: '授权类型', minWidth: 100},
                    {field: 'webServerRedirectUri', title: '重定向URI', minWidth: 250},
                    // {field: 'accessTokenValidity', title: '访问令牌有效时间值(单位秒)', minWidth: 100},
                    // {field: 'refreshTokenValidity', title: '更新令牌有效时间值(单位秒)', minWidth: 100},
                    {field: 'remark', title: '备注', minWidth: 100},
                    {field: 'updatedBy', title: '更新人', width: 100},
                    {field: 'updatedTime', title: '更新时间', templet: '<div>{{ layui.util.toDateString(d.updatedTime, "yyyy-MM-dd HH:mm:ss") }}</div>',  minWidth: 170, sort: true},
                    {title: '操作', width: 160, align: 'center', fixed: 'right', toolbar: '#client-table'}
                ]
            ],
            done: function (res, curr, count) {
                    //传入参数 得到当前渲染的数据，当前页码，数据总量

                    // if (res.code == '10017' || res.code == '10018') {
                    //     layer.msg(res.msg, {icon: 2, time: 1200, shade: [0.3, '#000', true]}, function(){
                    //         view.exit();
                    //     });
                    // }
            },
            error: function (obj, msg) {
                console.error(msg);
            },
            text: {
                none: '暂无相关数据'
            }
        });

        //监听工具条
        table.on('tool(LAY-client-table)', function (obj) {
            let line_data = obj.data;
            if (obj.event === 'del') {
                layer.confirm('真的删除行么？', function (index) {
                    let loadIndex = layer.load(0);
                    $.ajax({
                        url: ctx + '/oauth/client/delete',
                        type: 'POST',
                        data: {clientId: line_data.clientId},
                        success: function (res) {
                            if (res.code === '00000') {
                                //执行成功
                                layer.msg(res.msg, {icon: 1});
                                table.reload('LAY-client-table'); //数据刷新
                                layer.close(loadIndex); //关闭弹层
                            } else {
                                layer.msg(res.msg, {icon: 2, time: 3000, shade: [0.3, '#000', true]});
                            }
                        }
                    })
                });
            } else if (obj.event === 'edit') {
                let idx = layer.open({
                    type: 1,
                    area: '500px',
                    resize: false,
                    shadeClose: true,
                    title: '编辑',
                    content: `
                      <div class="layui-form" lay-filter="filter-client-layer" style="margin: 16px;">
                        <div class="demo-login-container">
                          <div class="layui-form-item">
                            <div class="layui-input-wrap">
                              <input type="hidden" name="clientId" value="`+line_data.clientId+`"/>
                              <textarea placeholder="请输入重定向地址" name="webServerRedirectUri" lay-verify="required" lay-reqtext="请输入重定向地址" autocomplete="off" lay-affix="clear" class="layui-textarea">`+line_data.webServerRedirectUri+`</textarea>
                            </div>
                          </div>
                          <div class="layui-form-item">
                            <div class="layui-col-xs6 layui-col-sm6 layui-col-md5">
                              &nbsp;
                            </div>
                            <div class="layui-col-xs6 layui-col-sm6 layui-col-md2">
                              <button class="layui-btn layui-bg-blue layui-btn-fluid" lay-submit lay-filter="client-update">提交</button>
                            </div>
                            <div class="layui-col-xs6 layui-col-sm6 layui-col-md5">
                              &nbsp;
                            </div>
                          </div>
                        </div>
                      </div>
                    `,
                    success: function(){
                        // 对弹层中的表单进行初始化渲染
                        form.render();
                        // 表单提交事件
                        form.on('submit(client-update)', function(data){
                            let field = data.field; // 获取表单字段值
                            let dd = {};
                            dd.clientId = field.clientId;
                            dd.webServerRedirectUri = field.webServerRedirectUri;

                            let loadIndex = layer.load(0);
                            $.ajax({
                                url: ctx + '/oauth/client/save',
                                type: 'POST',
                                data: JSON.stringify(dd),
                                contentType: 'application/json;charset=utf-8',
                                dataType: 'json',
                                success: function (res) {
                                    if (res.code === '00000') {
                                        //执行成功
                                        layer.msg(res.msg, {icon: 1});
                                        table.reload('LAY-client-table'); //数据刷新
                                        layer.close(loadIndex); //关闭弹层
                                        layer.close(idx); //关闭弹层
                                    } else {
                                        layer.msg(res.msg, {icon: 2, time: 3000, shade: [0.3, '#000', true]});
                                    }
                                }
                            })
                            return false; // 阻止默认 form 跳转
                        });
                    }
                });
            }
        });

        //监听刷新
        form.on('submit(LAY-client-refresh)', function (data) {
            let field = data.field;
            //执行重载
            table.reload('LAY-client-table', {
                where: {
                    param: {
                        tenantId: userId
                    }
                }
            });
        });
        // 监听新增
        form.on('submit(LAY-client-add)', function (data) {
            let idx = layer.open({
                type: 1,
                area: '500px',
                resize: false,
                shadeClose: true,
                title: '新增',
                content: `
                      <div class="layui-form" lay-filter="filter-client-layer" style="margin: 16px;">
                        <div class="">
                          <div class="layui-form-item">
<!--                            <label class="layui-form-label">登录名</label>-->
<!--                              <div class="layui-input-block">-->
<!--                              <input type="text" name="webServerRedirectUri" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input" lay-affix="clear">-->
<!--                            </div>-->
<!--                            <div class="layui-input-wrap">-->
<!--                              <div class="layui-input-prefix">-->
<!--                                <i class="layui-icon layui-icon-username"></i>-->
<!--                              </div>-->
<!--                              <input type="text" name="webServerRedirectUri" value="" lay-verify="required" placeholder="用户名" lay-reqtext="请填写用户名" autocomplete="off" class="layui-input" lay-affix="clear">-->
<!--                            </div>-->
                          </div>
                          <div class="layui-form-item">
                            <div class="layui-input-wrap">
                              <input type="text" name="appName" value="" lay-verify="required" placeholder="请输入应用名" autocomplete="off" class="layui-input" lay-affix="clear"/>
                            </div>
                          </div>
                          <div class="layui-form-item">
                            <div class="layui-input-wrap">
                              <textarea placeholder="请输入重定向地址" name="webServerRedirectUri" lay-verify="required" lay-reqtext="请输入重定向地址" autocomplete="off" lay-affix="clear" class="layui-textarea"></textarea>
                            </div>
                          </div>
                          <div class="layui-form-item">
                            <div class="layui-input-wrap">
                              <textarea placeholder="备注" name="remark" autocomplete="off" lay-affix="clear" class="layui-textarea"></textarea>
                            </div>
                          </div>
                          <div class="layui-form-item">
                            <div class="layui-col-xs6 layui-col-sm6 layui-col-md5">
                              &nbsp;
                            </div>
                            <div class="layui-col-xs6 layui-col-sm6 layui-col-md2">
                              <button class="layui-btn layui-bg-blue layui-btn-fluid" lay-submit lay-filter="client-add">提交</button>
                            </div>
                            <div class="layui-col-xs6 layui-col-sm6 layui-col-md5">
                              &nbsp;
                            </div>
                          </div>
                        </div>
                      </div>
                    `,
                success: function(){
                    // 对弹层中的表单进行初始化渲染
                    form.render();
                    // 表单提交事件
                    form.on('submit(client-add)', function(data){
                        let field = data.field; // 获取表单字段值
                        let dd = {};
                        dd.tenantId = userId;
                        dd.appName = field.appName;
                        dd.webServerRedirectUri = field.webServerRedirectUri;
                        dd.remark = field.remark;

                        let loadIndex =layer.load(0);
                        $.ajax({
                            url: ctx + '/oauth/client/save',
                            type: 'POST',
                            data: JSON.stringify(dd),
                            contentType: 'application/json;charset=utf-8',
                            dataType: 'json',
                            success: function (res) {
                                if (res.code === '00000') {
                                    //执行成功
                                    layer.msg(res.msg, {icon: 1});
                                    table.reload('LAY-client-table'); //数据刷新
                                    layer.close(loadIndex); //关闭弹层
                                    layer.close(idx); //关闭弹层
                                } else {
                                    layer.msg(res.msg, {icon: 2, time: 3000, shade: [0.3, '#000', true]});
                                }
                            }
                        });
                        return false; // 阻止默认 form 跳转
                    });
                }
            });
        });

        // 事件
        util.on('lay-on', {
            'logout': function (obj) {
                let loadIndex =layer.load(0);
                $.ajax({
                    url: ctx + '/oauth/user/logout',
                    type: 'POST',
                    success: function (res) {
                        if (res.code === '00000') {
                            //执行成功
                            layer.msg('注销登录成功！', {
                                offset: '15px'
                                , icon: 1
                                , time: 1000
                            }, function () {
                                sessionStorage.clear();
                                layer.close(loadIndex); //关闭弹层
                                location.href = ctx + '/oauth/login/form';
                            });
                        } else {
                            layer.msg(res.msg, {icon: 2, time: 3000, shade: [0.3, '#000', true]});
                        }
                    }
                })
            }
        });

    });
</script>
</body>
</html>